package com.imooc.o2o.web.shopadmin;

import com.imooc.o2o.dto.Result;
import com.imooc.o2o.entity.ProductCategory;
import com.imooc.o2o.entity.Shop;
import com.imooc.o2o.enums.ProductCategoryStateEnum;
import com.imooc.o2o.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 徐老板
 * @date 2020/1/21   22:13
 */
@Controller
@RequestMapping("/shopadmin")
public class ProductCategoryController {
    @Autowired
    ProductCategoryService productCategoryService;

    @GetMapping("/getproductcategorylist")
    @ResponseBody
//    这里因为只返回一个值 所以用了result 也可以用map
    public Result<List<ProductCategory>> getProductCategoryList(HttpServletRequest request){
        //to be remove
        /**
         * 可以从ShopManagementController中的http://localhost:8080/shopadmin/getshopmanagementinfo获得
         * 这里设置了已经
         */
        Shop shop = new Shop();
        shop.setShopId(15);
        request.getSession().setAttribute("currentShop", shop);
        Shop currentShop= (Shop) request.getSession().getAttribute("currentShop");
        List<ProductCategory> list=null;
        if (currentShop!=null&&currentShop.getShopId()>0){
            list = productCategoryService.getProductCategoryList(currentShop.getShopId());
            return new Result<>(true,list );
        }else {
            ProductCategoryStateEnum innerError = ProductCategoryStateEnum.INNER_ERROR;
            return new Result<List<ProductCategory>>(false, innerError.getMsg(),innerError.getCode() );
        }
    }


}
